<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="知识库名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入知识库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="嵌入模型" prop="embeddingModelName">
        <el-input
          v-model="queryParams.embeddingModelName"
          placeholder="请输入嵌入模型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 知识库列表 -->
    <el-table v-loading="loading" :data="knowledgeBaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="知识库ID" align="center" prop="id" width="200" />
      <el-table-column label="知识库名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="嵌入模型" align="center" prop="embeddingModelName" />
      <el-table-column label="重排模型" align="center" prop="rerankModelName" />
      <el-table-column label="配置模式" align="center" prop="confgModel" />
      <el-table-column label="分块大小" align="center" prop="chunkSize" />
      <el-table-column label="重叠大小" align="center" prop="overlapSize" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="知识库名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入知识库名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="嵌入模型" prop="embeddingModelName">
          <el-select v-model="form.embeddingModelName" placeholder="请选择嵌入模型">
            <el-option label="text-embedding-v2" value="text-embedding-v2" />
            <el-option label="text-embedding-v4" value="text-embedding-v4" />
          </el-select>
        </el-form-item>
        <el-form-item label="重排模型" prop="rerankModelName">
          <el-select v-model="form.rerankModelName" placeholder="请选择重排模型">
            <el-option label="gte-rerank-hybrid" value="gte-rerank-hybrid" />
          </el-select>
        </el-form-item>
        <el-form-item label="配置模式" prop="confgModel">
          <el-select v-model="form.confgModel" placeholder="请选择配置模式">
            <el-option label="推荐" value="recommend" />
          </el-select>
        </el-form-item>
        <el-form-item label="分块大小" prop="chunkSize">
          <el-input-number v-model="form.chunkSize" :min="100" :max="2000" />
        </el-form-item>
        <el-form-item label="重叠大小" prop="overlapSize">
          <el-input-number v-model="form.overlapSize" :min="0" :max="500" />
        </el-form-item>
        <el-form-item label="重排最小分数" prop="rerankMinScore">
          <el-input v-model="form.rerankMinScore" placeholder="请输入重排最小分数" />
        </el-form-item>
        <el-form-item label="启用重写" prop="enableRewrite">
          <el-switch v-model="form.enableRewrite" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 知识库详情对话框 -->
    <el-dialog title="知识库详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="知识库ID">{{ detailForm.id }}</el-descriptions-item>
        <el-descriptions-item label="知识库名称">{{ detailForm.name }}</el-descriptions-item>
        <el-descriptions-item label="描述" :span="2">{{ detailForm.description }}</el-descriptions-item>
        <el-descriptions-item label="嵌入模型">{{ detailForm.embeddingModelName }}</el-descriptions-item>
        <el-descriptions-item label="重排模型">{{ detailForm.rerankModelName }}</el-descriptions-item>
        <el-descriptions-item label="配置模式">{{ detailForm.confgModel }}</el-descriptions-item>
        <el-descriptions-item label="分块大小">{{ detailForm.chunkSize }}</el-descriptions-item>
        <el-descriptions-item label="重叠大小">{{ detailForm.overlapSize }}</el-descriptions-item>
        <el-descriptions-item label="重排最小分数">{{ detailForm.rerankMinScore }}</el-descriptions-item>
        <el-descriptions-item label="启用重写">{{ detailForm.enableRewrite ? '是' : '否' }}</el-descriptions-item>
        <el-descriptions-item label="存储实例ID">{{ detailForm.sinkInstanceId }}</el-descriptions-item>
        <el-descriptions-item label="存储区域">{{ detailForm.sinkRegion }}</el-descriptions-item>
        <el-descriptions-item label="存储类型">{{ detailForm.sinkType }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKnowledgeBase, getKnowledgeBase, delKnowledgeBase, addKnowledgeBase, delKnowledgeBaseBatch } from "@/api/ai"

export default {
  name: "KnowledgeBase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识库表格数据
      knowledgeBaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        embeddingModelName: null
      },
      // 表单参数
      form: {},
      // 详情表单参数
      detailForm: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "知识库名称不能为空", trigger: "blur" }
        ],
        embeddingModelName: [
          { required: true, message: "嵌入模型不能为空", trigger: "change" }
        ],
        rerankModelName: [
          { required: true, message: "重排模型不能为空", trigger: "change" }
        ],
        confgModel: [
          { required: true, message: "配置模式不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询知识库列表 */
    getList() {
      this.loading = true
      listKnowledgeBase(this.queryParams).then(response => {
        this.knowledgeBaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        description: null,
        embeddingModelName: "text-embedding-v2",
        rerankModelName: "gte-rerank-hybrid",
        confgModel: "recommend",
        chunkSize: 500,
        overlapSize: 100,
        enableRewrite: true,
        rerankMinScore: "0.01"
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加知识库"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getKnowledgeBase(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改知识库"
      })
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const id = row.id
      getKnowledgeBase(id).then(response => {
        this.detailForm = response.data
        this.detailOpen = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 修改功能暂不支持
            this.$modal.msgError("暂不支持修改知识库")
          } else {
            addKnowledgeBase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除知识库编号为"' + ids + '"的数据项？').then(function() {
        return delKnowledgeBase(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>